package com.com.hefei.wdp.example.contorller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
//hystrix全局配置熔断配置 低于局部配置
@DefaultProperties(defaultFallback="timeoutHandler",commandProperties = {
        @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="5000") //超过三秒
})
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;
    //    未添加 @EnableDiscoveryClient 时 的调用 ,只能通过IP调用
//    private final String producerServer="http://localhost:8061/";
//    添加 @EnableDiscoveryClient 时 的调用 ，可以通过服务名调用
    private final String producerServer="http://PRODUCER";

    @GetMapping("/appInfo")
    // 局部控制
//    @HystrixCommand(fallbackMethod = "timeoutHandler",commandProperties = {
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="5000") //超过三秒
//    })
    @HystrixCommand  //@DefaultProperties + @HystrixCommand  实现全局配置
    public String getAppInfo() {
        return restTemplate.getForObject(producerServer+"/appInfo",String.class);
    }

    public String timeoutHandler(){
        return "客户端超时。。。。";
    }
}
